from CvPythonExtensions import *

import GerikSpells
from GerikSpells import GerikSpellBase
from GSGroupPolicies import GSAll as GSGroup
from GSHandlerPolicies import GSImmediateNoTarget as GSHandler
from GSResultsPolicies import GSMissionNoTarget as GSResults

import GerikSpellsUtils

gc = CyGlobalContext()
localtext = CyTranslator()

class GhostCloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iGhostUnitType = gc.getInfoTypeForString("UNIT_GHOST")
        self.m_iGhostResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_GHOST_CLOAKING")
        self.m_iGhostResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iGhostResearchNeeded).getBuildingToBuildAt()
        self.m_iCastingCost = -1 * gc.getDefineINT("CLOAK_CASTING_COST")
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iGhostResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iGhostResearchBuildingToBuildAt).getDescription()) )


    def getHelpText(self, pGroup, pPlot):
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText
            
    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        # For ghosts only.
        if not pCastingUnit.getUnitType() == self.m_iGhostUnitType:
            return False

        # Don't allow for a unit that is already cloaked.
        if pCastingUnit.isCloakBurningEnergy():
            return False

        # Show this for all ghosts, only enable it if they have the research.
        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iGhostResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        bShowNotEnoughEnergy = False
        for pUnit in pCastingGroup.getUnits():
            if pUnit.currEnergy() < self.m_iCastingCost:
                bShowNotEnoughEnergy = True
            else:
                pUnit.changeEnergy(self.m_iCastingCost)
                pUnit.setCloakBurningEnergy(True)

        if bShowNotEnoughEnergy:
            pass

        # Don't continue the mission.
        return False

class WraithCloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

        self.m_iWraithUnitType = gc.getInfoTypeForString("UNIT_WRAITH")
        self.m_iWraithResearchNeeded = gc.getInfoTypeForString("PROJECT_TERRAN_WRAITH_CLOAKING")
        self.m_iWraithResearchBuildingToBuildAt = gc.getProjectInfo(self.m_iWraithResearchNeeded).getBuildingToBuildAt()
        self.m_iCastingCost = -1 * gc.getDefineINT("CLOAK_CASTING_COST")
        self.m_szNotValidText = localtext.getText( "TXT_KEY_CIVCRAFT_REQUIRES_RESEARCH_AT", (gc.getProjectInfo(self.m_iWraithResearchNeeded).getDescription(),
                                                                                             gc.getBuildingInfo(self.m_iWraithResearchBuildingToBuildAt).getDescription()) )

    def getHelpText(self, pGroup, pPlot):
        # Get whether the group can cloak but they can't 
        bValid = self.canGroupCast(pGroup, pPlot, False)
        if not bValid:
            return self.m_szNotValidText
            

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        # For Wraiths only.
        if not pCastingUnit.getUnitType() == self.m_iWraithUnitType:
            return False

        # Don't allow for a unit that is already cloaked.
        if pCastingUnit.isCloakBurningEnergy():
            return False

        # Show this for all ghosts, only enable it if they have the research.
        if bVisible:
            return True

        # Check if they have the research
        if not gc.getPlayer( pCastingUnit.getOwner() ).hasBuiltProject( self.m_iWraithResearchNeeded ):
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        bShowNotEnoughEnergy = False
        for pUnit in pCastingGroup.getUnits():
            if pUnit.currEnergy() < self.m_iCastingCost:
                bShowNotEnoughEnergy = True
            else:
                pUnit.changeEnergy(self.m_iCastingCost)
                pUnit.setCloakBurningEnergy(True)

        if bShowNotEnoughEnergy:
            pass

        # Don't continue the mission.
        return False

class Decloak(GerikSpellBase, GSGroup, GSHandler, GSResults):
    def __init__(self):
        GerikSpellBase.__init__(self)
        GSGroup.__init__(self)
        GSHandler.__init__(self)
        GSResults.__init__(self)

    def canUnitCast(self, pCastingUnit, pPlot, bVisible):
        if not pCastingUnit.isCloakBurningEnergy():
            return False

        return True
        
    def startSpellResults(self, pCastingGroup, pTarget):
        for pUnit in pCastingGroup.getUnits():
            pUnit.setCloakBurningEnergy(False)

        # Don't continue the mission
        return False


        

                        
            
        
            
            
